package com.itheima.prince.smartpeking.activity;

import android.app.Activity;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Build;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageButton;
import android.widget.ProgressBar;

import com.itheima.prince.smartpeking.R;

import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;

public class NewsDetailUI extends Activity implements View.OnClickListener {

    private ImageButton ib_newdetail_back;
    private ImageButton ib_newdetail_share;
    private ImageButton ib_newdetail_text;
    private WebView wv_newsdetail_web;
    private ProgressBar pb_newsdetail_progerssbar;
    private WebSettings settings;
    private int currentIndex =2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//            Window window = getWindow();
//            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
//                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
//            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
//                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
//                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
//            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
//            window.setStatusBarColor(Color.TRANSPARENT);
//            window.setNavigationBarColor(Color.TRANSPARENT);
//        }
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_news_detail_ui);
        initView();
        initData();
    }


    private void initView() {
        ib_newdetail_back = (ImageButton) findViewById(R.id.ib_newdetail_back);
        ib_newdetail_share = (ImageButton) findViewById(R.id.ib_newdetail_share);
        ib_newdetail_share.setOnClickListener(this);
        ib_newdetail_text = (ImageButton) findViewById(R.id.ib_newdetail_text);
        ib_newdetail_text.setOnClickListener(this);
        wv_newsdetail_web = (WebView) findViewById(R.id.wv_newsdetail_web);
        pb_newsdetail_progerssbar = (ProgressBar) findViewById(R.id.pb_newsdetail_progerssbar);

    }

    private void initData() {
        //获取传过来的数据
        String url = getIntent().getStringExtra("url");

        wv_newsdetail_web.loadUrl(url);
        settings = wv_newsdetail_web.getSettings();
        settings.setUseWideViewPort(true);// 启动双击缩放功能
        settings.setBuiltInZoomControls(true);//启用缩放功能
        settings.setJavaScriptEnabled(true);//启用js功能

        //监听webview来结束loading
        wv_newsdetail_web.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                pb_newsdetail_progerssbar.setVisibility(View.GONE);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ib_newdetail_back:
                finish();
                break;
            case R.id.ib_newdetail_text:
                showTextSize();

                break;
            case R.id.ib_newdetail_share:
                showShare();
                break;

            default:
                break;
        }
    }

    private void showTextSize() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        String[] item = new String[]{"超大号字体", "大号字体", "正常字体", "小号字体", "超小号字体"};
        builder.setSingleChoiceItems(item, currentIndex, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                currentIndex = which;
            }
        });
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                changeTextSize(currentIndex);
            }
        });
        builder.setNegativeButton("取消",null);
        //这两部必须加上
        AlertDialog alertDialog = builder.create();
        alertDialog.show();

    }

    private void changeTextSize(int which) {
        switch (which) {
            case 0:
                settings.setTextSize(WebSettings.TextSize.LARGEST);
                break;
            case 1:
                settings.setTextSize(WebSettings.TextSize.LARGER);
                break;
            case 2:
                settings.setTextSize(WebSettings.TextSize.NORMAL);
                break;
            case 3:
                settings.setTextSize(WebSettings.TextSize.SMALLER);
                break;
            case 4:
                settings.setTextSize(WebSettings.TextSize.SMALLEST);
                break;

            default:
                break;
        }
    }

    private void showShare() {
        ShareSDK.initSDK(this);
        OnekeyShare oks = new OnekeyShare();
        //关闭sso授权
        oks.disableSSOWhenAuthorize();

// 分享时Notification的图标和文字  2.5.9以后的版本不调用此方法
        //oks.setNotification(R.drawable.ic_launcher, getString(R.string.app_name));
        // title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间使用
        oks.setTitle("标题");
//        // titleUrl是标题的网络链接，仅在人人网和QQ空间使用
//        oks.setTitleUrl("http://sharesdk.cn");
//        // text是分享文本，所有平台都需要这个字段
        oks.setText("我是机智的分享文本");
//        //分享网络图片，新浪微博分享网络图片需要通过审核后申请高级写入接口，否则请注释掉测试新浪微博
//        oks.setImageUrl("http://f1.sharesdk.cn/imgs/2014/02/26/owWpLZo_638x960.jpg");
//        // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
        oks.setImagePath("/storage/emulated/0/Tencent/QQfile_recv/下载.jpg");//确保SDcard下面存在此张图片
//        // url仅在微信（包括好友和朋友圈）中使用
//        oks.setUrl("http://sharesdk.cn");
//        // comment是我对这条分享的评论，仅在人人网和QQ空间使用
//        oks.setComment("我是测试评论文本");
//        // site是分享此内容的网站名称，仅在QQ空间使用
//        oks.setSite("ShareSDK");
//        // siteUrl是分享此内容的网站地址，仅在QQ空间使用
//        oks.setSiteUrl("http://sharesdk.cn");

// 启动分享GUI
        oks.show(this);
    }

}
